const express = require("express")
const router = express.Router()
const { login, register } = require("../contorl/user")
const queryUsername = require("../middleware/user")

router.post("/register", queryUsername, (req, res, next) => {
  const { username, password, realname } = req.body
  register(username, password, realname).then(result => {
    res.json(result)
  })
})

router.post("/login", (req, res, next) => {
  const { username, password } = req.body
  login(username, password).then(result => {
    if(result.code === 0){
      req.session.username = result.data.username
      req.session.realname = result.data.realname
    }
    res.json(result)
  })
})


module.exports = router